package com.taomeishu.mailcrazy.checkers.gmail;

import org.json.JSONObject;

import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.Mail;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public class GmailResultParser implements ResultParser {

	@Override
	public CheckResult parse(ClientResponse clientResponse, String username, String domainName) throws ResultParseException {
		CheckResult result = new CheckResult(true);
		Mail mail = new Mail(username, domainName);
		result.setMail(mail);
		String s = null;
		try {
			s = new String(clientResponse.getData(), "utf-8");
			JSONObject json = new JSONObject(s);
			JSONObject input01 = json.getJSONObject("input01");
			boolean valid = input01.getBoolean("Valid");
			if (valid) {
				result.addNotExistMail(mail);
			} else {
				// exist
				result.addExistMail(mail);
			}
		} catch (Exception e) {
			throw new ResultParseException(s, e);
		}
		return result;
	}

}
